package group32.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Login extends Composite implements LoginService, ValueChangeHandler<String>{
	int userid=-1;
	private LoginServiceAsync pService = GWT
	.create(LoginService.class);
	VerticalPanel dialogVPanel = new VerticalPanel();
	Button bLogin= new Button();
	TextBox nameField = new TextBox();
	PasswordTextBox passField = new PasswordTextBox();
	Hyperlink registration = new Hyperlink("Register User" ,"register");
	TextBox error = new TextBox();
	protected static String username;
	public Login(){
		History.newItem("Login");
		initWidget(dialogVPanel);
		dialogVPanel.addStyleName("dialogVPanel");
		//dialogVPanel.add(new HTML("<h3><center><style='color:blue;' />Login</center></h3>"));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		
		bLogin.setText("Login");
		error.setEnabled(false); error.setVisible(false);
		//nameField.setText("");
		//passField.setText("");
		HorizontalPanel h1= new HorizontalPanel();
		h1.add(new Label("Username: "));h1.add(nameField);
		nameField.setWidth("150px");
		h1.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		h1.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		dialogVPanel.add(h1);
		HorizontalPanel h2= new HorizontalPanel();
		h2.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		h2.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		passField.setWidth("150px");
		h2.add(new Label("Password: "));h2.add(passField);
		dialogVPanel.add(h2);
		dialogVPanel.add(bLogin);
		dialogVPanel.add(registration);
		dialogVPanel.add(error);
		nameField.setValue("");
		passField.setValue("");
		error.setWidth("200px");
		History.addValueChangeHandler(this);
		
		bLogin.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	//if((!username.equals(""))&& (!passField.equals(""))){
	           username= nameField.getValue();
	           String password= passField.getValue();
	           login(username, password);
	        	//}
	            
	        }          
	    });
		
	
		
	}
	@SuppressWarnings("rawtypes")
	public void onValueChange(ValueChangeEvent event){
		if(History.getToken().equals("register"))
			FlowControl.go(new Registration());
	}
	
	public String greetServer(String name) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}

	public int login(String name, String password)
			throws IllegalArgumentException {
		pService.login(name,password, new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
					}
					public void onSuccess(Integer result) {
						if(result!=-1)FlowControl.go(new HomePage(result,username));
						else {
							error.setVisible(true);
							error.setText("username/password is incorrect");
						}
					}
		});
		return -1;
	}
		
}//class ends
